package com.relatev.minecraft.TRMobSpawn;

import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;

public class ConfigManager {

    private static File ConfigFile;
    private static YamlConfiguration config;
    private static Gson gson;

    public static void init() {
        gson = new Gson();
        ConfigFile = new File(TRMobSpawn.MainPlugin.getDataFolder(), "config.yml");
        if (ConfigFile.exists() == false) {
            ConfigFile.getParentFile().mkdirs();
            try {
                ConfigFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        config = YamlConfiguration.loadConfiguration(ConfigFile);
        if (config.getInt("Version") != 100) {
            config.set("Version", 100);
            for (EntityType et : EntityType.values()) {
                if (et.isAlive()) {
                    config.set("CustomSpawn.world." + et.name(), gson.toJson(new TRMobInfo(10, 10, "TR_" + et.name())));
                }
            }
            try {
                config.save(ConfigFile);
            } catch (IOException ex) {
                Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static TRMobInfo getSpawnInfo(World world, EntityType type) {
        if (config.contains("CustomSpawn." + world.getName() + "." + type.name())) {
            return gson.fromJson(config.getString("CustomSpawn." + world.getName() + "." + type.name()), TRMobInfo.class);
        }
        return null;
    }
}
